
import { NotifyService } from '@abp/notify/notify.service';
import { Component, Injector, ViewChild, ViewEncapsulation, OnInit } from '@angular/core';
import { AppConsts } from '@shared/AppConsts';
import { ActivatedRoute } from '@angular/router';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { Http } from '@angular/http';
import { EntityDtoOfInt64, TokenAuthServiceProxy, AddressListDto, WULINGAddressServiceProxy } from '@shared/service-proxies/service-proxies';
import { FileUploader, FileUploaderOptions, FileItem } from 'ng2-file-upload';
import { IAjaxResponse } from '@abp/abpHttpInterceptor';
import { TokenService } from '@abp/auth/token.service';

@Component({
  selector: 'app-wuling',
  templateUrl: './wuling.component.html',
  styleUrls: ['./wuling.component.css'],
  encapsulation: ViewEncapsulation.None,
  animations: [appModuleAnimation()]
})
export class WulingComponent extends AppComponentBase implements OnInit {
  @ViewChild('dataTable') dataTable: Table;
  @ViewChild('paginator') paginator: Paginator;

  //Filters
  serviceStationCode = '';
  serviceStationName = '';
  excelUploader: FileUploader;
  advancedFiltersAreShown = false;
    filterText = '';
    selectedPermission = '';
    role: number = undefined;
    onlyLockedUsers = false;

  constructor(
    injector: Injector,
    private _http: Http,
    private _wulingServiceProxy: WULINGAddressServiceProxy,
    private _notifyService: NotifyService,
    private _tokenAuth: TokenAuthServiceProxy,
    private _activatedRoute: ActivatedRoute,
    private _tokenService: TokenService
  ) {
    super(injector);
  }

  ngOnInit(): void {

    this.initUploaders();
  }

  initUploaders(): void {
    this.excelUploader = this.createUploader(
      '/WULINGAddress/ImportFromExcel',
      result => {
        this.appSession.tenant.logoFileType = result.fileType;
        this.appSession.tenant.logoId = result.id;
      }
    );




  }

  createUploader(url: string, success?: (result: any) => void): FileUploader {
    const uploader = new FileUploader({ url: AppConsts.remoteServiceBaseUrl + url });

    uploader.onAfterAddingFile = (file) => {
      file.withCredentials = false;
      uploader.uploadAll();
    };

    uploader.onSuccessItem = (item, response, status) => {
      const ajaxResponse = <IAjaxResponse>JSON.parse(response);
      if (ajaxResponse.success) {
        this.notify.info(this.l('SavedSuccessfully'));
        if (success) {
          success(ajaxResponse.result);
        }
      } else {
        this.message.error(ajaxResponse.error.message);
      }
    };

    const uploaderOptions: FileUploaderOptions = {};
    uploaderOptions.authToken = 'Bearer ' + this._tokenService.getToken();
    uploaderOptions.removeAfterUpload = true;
    uploader.setOptions(uploaderOptions);
    return uploader;
  }

  getAddresss(event?: LazyLoadEvent) {
    if (this.primengTableHelper.shouldResetPaging(event)) {
      this.paginator.changePage(0);

      return;
    }

    this.primengTableHelper.showLoadingIndicator();

    this._wulingServiceProxy.getAddresssAsync(
      this.serviceStationCode,
      this.serviceStationName,
      this.primengTableHelper.getSorting(this.dataTable),
      this.primengTableHelper.getMaxResultCount(this.paginator, event),
      this.primengTableHelper.getSkipCount(this.paginator, event)
    ).subscribe(result => {
      this.primengTableHelper.totalRecordsCount = result.totalCount;
      this.primengTableHelper.records = result.items;
      this.primengTableHelper.hideLoadingIndicator();
    });
  }

  importExcel(): void {
  }
  reloadPage(): void {
    this.paginator.changePage(this.paginator.getPage());
  }
  delete(rec: AddressListDto): void {

    this.message.confirm(
      rec.serviceStationName,
      '确定删除吗',
      (isConfirmed) => {
        if (isConfirmed) {
          this._wulingServiceProxy.deleteAddressAsync(rec.id)
            .subscribe(() => {
              this.reloadPage();
              this.notify.success(this.l('SuccessfullyDeleted'));
            });
        }
      }
    );

  }


}
